<template>
	<view class="contain">
		
		<block v-if="hasLogin">
			<block v-if="dataList.data[0]!=undefined">
				<view class="w-[98%] m-auto flex justify-between" v-for="item in dataList.data">
					<view class="fonttext">{{item.account}}</view>
					<view class="fonttext">{{item.pwd}}</view>
					<view class="fonttext blod yuyuebtn lookbg" v-if="item.status!=null && item.status!=''" @click="openPDF(item.status)">查看报告</view>
					<view class="fonttext red" v-else @click="reBind">链接无效</view>
					<view class="fonttext blod yuyuebtn" @click="gogo(item)">预约体检</view>
				</view>			
			</block>
			<block v-else>
				<view class="flex justify-center w-[100%] mt-5">
					<view><u-button type="primary" shape="circle" @click="goBind">还没绑定用户，点击前往绑定</u-button></view>
				</view>			
			</block>			
		</block>
		<block v-else> 
			<view  class="flex justify-center w-[50%] mt-5 m-auto"><u-button type="primary" shape="circle" @click="goLogin">登录后查看</u-button></view>
		</block>
		

		
		<view class="w-[98%] m-auto my-5" @click="pciGo"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/home/ykd2.png" mode="widthFix" style="width:100%;"></image></view>

		<!-- <view>{{testlink}}</view>
		<view>错误信息:{{errormsg}}</view>
		<view>下载信息: {{download}}</view> -->
		<!-- <view class="logout">
			<u-button type="primary" shape="shape" text="退出登录" @click="logout"></u-button>
		</view> -->
	</view>
	
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'
const store = userStore();
let { hasLogin,userData } = userStore();



let dataList=reactive({data:[]});
let testlink=ref('');
let errormsg=ref('');
let download=ref('');
const openPDF=(pdfUrl:string)=>{
	// store.backRouter=pdfUrl;
	// uni.$tools.GoUrl("/pages/webview/webview");
	if(!/\S/.test(pdfUrl)){
		uni.$tools.Toast("报告链接无效");
		return;
	}
	uni.showLoading({
		title:'正在打开报告'
	})
	testlink.value=pdfUrl;
	console.log(pdfUrl);
	setTimeout(()=>{
		uni.hideLoading();
	},2000);
	//文档
	uni.downloadFile({
	  url:pdfUrl, //'https://example.com/somefile.pdf'
	  //url:encodeURIComponent("https://guangxins.oss-cn-shenzhen.aliyuncs.com/pdfs/白峻林测评报告-1798995118254985216.pdf"),
	  success:res=>{
		  console.log("下载结果:",res);
		  download.value=res;
		var filePath = res.tempFilePath;
		uni.openDocument({
		  filePath: filePath,
		  showMenu: true,
		  success:res=>{
			console.log('打开文档成功',res);
			errormsg.value=res;
		  },
		  fail:err=>{
			 console.log('打开文档失败',err); 
			 //uni.$tools.Toast(err);
			 errormsg.value=err;
		  }
		});
	  }
	});	
}

const goLogin=()=>{
	uni.$tools.GoUrl("/pages/login/login",1);
}

const reBind=()=>{
	uni.showModal({
		title:"温馨提示",
		content:"报告链接无效，是否重新绑定",
		showCancel:true,
		success:res=>{
			if(res.confirm){
				goBind();
			}
		},
		fail:err=>{
			
		}
	})
}

const goBind=()=>{
	if(!checkLogin()){
		return;
	}//检查登录
	uni.$tools.GoUrl("/pages/binduser/binduser");
}

const gogo=(items:any)=>{
	userData.bindusername=items.account;
	userData.binduseridcard=items.pwd;
	uni.$tools.GoUrl("/pages/word/word");
}

const pciGo=()=>{
	if(dataList.data[0]!=undefined){
		userData.bindusername=dataList.data[0].account;
		userData.binduseridcard=dataList.data[0].pwd;
		uni.$tools.GoUrl("/pages/word/word");			
	}
}


const logout=()=>{
	uni.showModal({
		title: '操作提示',
		content: '是否退出登录',
		success:res=>{
			if (res.confirm) {
				uni.clearStorageSync();
				uni.$tools.GoUrl("/pages/login/login",0);
			} else if (res.cancel) {
			}
		}
	});
}

//获取绑定数据
const getBindData=()=>{
	let paraData={
		id:userData.userinfo.id
	}
	uni.$tools.Reqeust('/api/getBindData',paraData,'POST').then((res:any)=>{
		console.log("获取绑定数据结果",res);
		if(res.data.code===200){
			dataList.data=[];
			store.targetUser=[];
			dataList.data.push(res.data.cardData);
			store.targetUser.push(res.data.cardData);
		}
	});
}



const checkLogin=()=>{
	let msg="请登录后操作";
	if(hasLogin===false || userData.userinfo===null){
			msg="请登录后购买，点击【确定】登录";
			uni.showModal({
					title:"温馨提示",
					content:msg,
					showCancel:true,
					success:res=>{
						if(res.confirm){
							uni.$tools.GoUrl("/pages/login/login",1);
						}
					}
			})
			return false;
	}
	return true;
}

onLoad((e:any)=>{

});

onShow(()=>{
	// if(userData.userinfo===null || hasLogin===false){
	// 	uni.$tools.Toast("会话失效请重新登录");
	// 	setTimeout(()=>{
	// 		uni.$tools.GoUrl('/pages/login/login',0);
	// 	},1000);
	// 	return;
	// }
	if(store.targetUser[0]!=undefined){
		console.log("读取缓存");
		dataList.data=store.targetUser;
	}else{
		//根据绑定的身份证去查询接口
		//用户当前id先查询userinfo绑定的身份证
		//再用身份证去查询对应的绑定数据
		getBindData();		
	}
	//console.log("opnePDF onShow",store.targetUser)
})
</script>

<style lang='scss' scoped>
	@import "openPDF.scss";
</style>